package com.imoocdl.controller;

import com.imoocdl.dataobject.ProductCategory;
import com.imoocdl.dataobject.ProductInfo;
import com.imoocdl.dto.OrderDtDTO;
import com.imoocdl.exception.SellException;
import com.imoocdl.form.ProductForm;
import com.imoocdl.service.CategoryService;
import com.imoocdl.service.ProductService;
import com.imoocdl.utils.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/7/24.
 */
@Controller
@RequestMapping("/seller/product")
@Slf4j
public class SellerProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    //@Cacheable(cacheNames = "product",key = "123")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                             @RequestParam(value = "size",defaultValue = "4") Integer size
            , Map<String,Object> map){
        PageRequest pageRequest = new PageRequest(page-1,size);
        Page<ProductInfo> productInfoPage =  productService.findAll(pageRequest);
        map.put("productInfoPage",productInfoPage);
        map.put("currentPage",page);
        map.put("size",size);
        return new ModelAndView("product/list",map);
    }
    @GetMapping("/on_sale")
    public ModelAndView onSale(@RequestParam(value = "orderId") String orderId
                                ,Map<String,Object> map){
        try{
            productService.onSale(orderId);
        }catch (SellException e){
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }
        map.put("msg","操作成功");
        map.put("url","/sell/seller/order/list");
        return new ModelAndView("common/success",map);
    }

    @GetMapping("/off_sale")
    public ModelAndView offSale(@RequestParam(value = "orderId") String orderId
            ,Map<String,Object> map){
        try{
            productService.offSale(orderId);
        }catch (SellException e){
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }
        map.put("msg","操作成功");
        map.put("url","/sell/seller/order/list");
        return new ModelAndView("common/success",map);
    }

    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "productId",required = false) String productId
            ,Map<String,Object> map){
        if(!StringUtils.isEmpty(productId)){
            ProductInfo productInfo = productService.findOne(productId);
            map.put("productInfo",productInfo);
        }
        //查询所有类目
        List<ProductCategory> categoryList = categoryService.findAll();
        map.put("categoryList",categoryList);
        return new ModelAndView("product/index",map);
    }

    @PostMapping("/save")
    //@CachePut(cacheNames = "product",key = "123")  //更新缓存 但是返回的对象不符合
    @CacheEvict(cacheNames = "product",key = "123")  //清楚对应key的redis缓存内容
    public ModelAndView save(@Valid ProductForm productForm,
                             BindingResult bindingResult,Map<String ,Object> map){
            if(bindingResult.hasErrors()){
                map.put("msg",bindingResult.getFieldError().getDefaultMessage());
                map.put("url","/sell/seller/product/index");
                return new ModelAndView("common/error",map);
            }
        ProductInfo productInfo = new ProductInfo();
        try{
                if(!StringUtils.isEmpty(productForm.getProductId())){
                    productInfo = productService.findOne(productForm.getProductId());
                }else{
                    productInfo.setProductId(KeyUtil.genUniqueKey());
                }

            BeanUtils.copyProperties(productForm,productInfo);
            productService.save(productInfo);
        }catch(SellException e){
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/product/index");
            return new ModelAndView("common/error",map);
        }
        map.put("msg","操作成功");
        map.put("url","/sell/seller/product/list");
        return new ModelAndView("common/success",map);

    }
}
